package
{
	import adobe.utils.CustomActions;
	import away3d.lights.PointLight3D;
	import com.quasimondo.bitmapdata.CameraBitmap;
	import flash.geom.Point;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import jp.maaash.ObjectDetection.ObjectDetector;
	import jp.maaash.ObjectDetection.ObjectDetectorEvent;
	import jp.maaash.ObjectDetection.ObjectDetectorOptions;
	
	public class FaceDetector_Camera extends Sprite
	{
		
		private var detector    :ObjectDetector;
		private var options     :ObjectDetectorOptions;
		
		private var view :Sprite;
		private var faceRectContainer :Sprite;
		private var tf :TextField;
		
		private var camera:CameraBitmap;
		private var detectionMap:BitmapData;
		private var drawMatrix:Matrix;
		private var scaleFactor:int = 4;
		private var w:int = 600;
		private var h:int = 600;
		
		private var lastTimer:int = 0;
		
		private var lastX:Number = 0;
		private var lastY:Number = 0; 
		
		private var centerPoint:Point = new Point();
		
		public var cpX:Number;
		public var cpY:Number;
		public var cpW:Number;
		
		//private var scene:LensShiftScene;
		//private var scene:FT2D;
		

		public function FaceDetector_Camera() {
			
			trace("face dectctor called");
			
			//addScene();
			
			initUI();
			initDetector();
			
			
			
		}

		private function initUI():void{
			//stage.scaleMode = StageScaleMode.NO_SCALE;
		//	stage.align = StageAlign.TOP_LEFT;

			trace("face detector initUI called");
			view = new Sprite;
			addChild(view);

			camera = new CameraBitmap( w, h, 15 );
			camera.addEventListener( Event.RENDER, cameraReadyHandler );
			var cameraOutPut:Bitmap = new Bitmap(camera.bitmapData);
			cameraOutPut.alpha = .0;
			view.addChild(cameraOutPut);
			
			detectionMap = new BitmapData( w / scaleFactor, h / scaleFactor, false, 0 );
			drawMatrix = new Matrix( 1/ scaleFactor, 0, 0, 1 / scaleFactor );
			
			faceRectContainer = new Sprite;
			view.addChild( faceRectContainer );
		}

		private function cameraReadyHandler( event:Event ):void
		{
			detectionMap.draw(camera.bitmapData,drawMatrix,null,"normal",null,true);
			detector.detect( detectionMap );
		}

		private function initDetector():void
		{
			trace("face detector initDetector called");
			detector = new ObjectDetector();
			
			var options:ObjectDetectorOptions = new ObjectDetectorOptions();
			options.min_size  = 30;
			detector.options = options;
			detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, detectionHandler );
		}
		
		
		
		private function detectionHandler( e :ObjectDetectorEvent ):void
		{
			trace("detection handler");
			var g :Graphics = faceRectContainer.graphics;
			g.clear();
			var cameraPassX:Number;
			var cameraPassY:Number;
			if( e.rects ){
				g.lineStyle( 2, 0x009900 );	// black 2pix
				e.rects.forEach( function( r :Rectangle, idx :int, arr :Array ) :void {
					centerPoint.x = (r.x * scaleFactor) + (r.width * scaleFactor * .5);
					centerPoint.y = (r.y * scaleFactor) + (r.height * scaleFactor * .5);
					g.drawRect( r.x * scaleFactor, r.y * scaleFactor, r.width * scaleFactor, r.height * scaleFactor );
					g.beginFill(0x990099);
					g.drawCircle(centerPoint.x, centerPoint.y, 5);
					//scene.setCamera(centerPoint.x, centerPoint.y);
					cpX = centerPoint.x;
					cpY = centerPoint.y;
					cpW = r.width * scaleFactor;
					//scene.updateAngle(0-centerPoint.x + 300, 0-centerPoint.y + 300,  r.width * scaleFactor);
					trace("center is  " + centerPoint.x + 300);
				} );
			}
			
		}
/*
		public function addScene():void
		{
			
			trace("face detector addScene called");
			scene = new LensShiftScene();
			scene.x = 300;
			scene.y = 300;
			//scene = new FT2D();
			this.addChild(scene);
		}
		*/

	
	}
}
